package com.zzw.controller;

import com.github.pagehelper.PageInfo;
import com.zzw.bean.*;
import com.zzw.service.OrderService;
import com.zzw.service.ShipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shipment")
public class ShipmentController {

    //@Autowired
    //private ShipmentService shipmentService;
    //
    //@Autowired
    //private OrderService orderService;
    //
    ///**
    // * 跳转到发货单列表页面（用于从订单管理跳转）
    // */
    //@GetMapping("/list")
    //public String list() {
    //    return "shipment/list";
    //}
    //
    ///**
    // * 获取发货单列表数据
    // */
    //@ResponseBody
    //@GetMapping("/getData")
    //public Msg getData(
    //        @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
    //        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
    //        Shipment shipment) {
    //
    //    PageInfo<Shipment> pageInfo = shipmentService.getShipmentList(shipment, pageNum, pageSize);
    //
    //    return Msg.success().add("pageInfo", pageInfo);
    //}
    //
    ///**
    // * 创建发货单
    // */
    //@ResponseBody
    //@PostMapping("/create")
    //public Msg create(@RequestBody Shipment shipment) {
    //    int id = shipmentService.createShipment(shipment);
    //    return Msg.success().add("id", id);
    //}
    //
    ///**
    // * 确认发货
    // */
    //@ResponseBody
    //@PutMapping("/confirm/{id}")
    //public Msg confirm(@PathVariable Integer id) {
    //    int result = shipmentService.confirmShipment(id);
    //    if (result > 0) {
    //        return Msg.success();
    //    } else {
    //        return Msg.fail("确认发货失败");
    //    }
    //}
    //
    ///**
    // * 获取发货单详情
    // */
    //@ResponseBody
    //@GetMapping("/detail/{id}")
    //public Msg detail(@PathVariable Integer id) {
    //    Shipment shipment = shipmentService.getShipmentDetailById(id);
    //    if (shipment == null) {
    //        return Msg.fail("发货单不存在");
    //    }
    //    return Msg.success().add("shipment", shipment);
    //}
    //
    ///**
    // * 根据订单ID创建发货单
    // */
    //@ResponseBody
    //@PostMapping("/createFromOrder/{orderId}")
    //public Msg createFromOrder(@PathVariable Integer orderId, @RequestBody Shipment shipmentInfo) {
    //    // 获取订单详情
    //    Order order = orderService.getOrderDetailById(orderId);
    //    if (order == null) {
    //        return Msg.fail("订单不存在");
    //    }
    //
    //    // 检查订单状态是否为已确认
    //    if (order.getStatus() != 1 && order.getStatus() != 2) {
    //        return Msg.fail("只有已确认或已部分发货的订单才能生成发货单");
    //    }
    //
    //    // 创建发货单
    //    Shipment shipment = new Shipment();
    //    shipment.setShipmentNo(shipmentService.generateShipmentNo());
    //    shipment.setOrderId(orderId);
    //    shipment.setStatus(0); // 待发货
    //    shipment.setShippingAddress(shipmentInfo.getShippingAddress());
    //    shipment.setContactName(shipmentInfo.getContactName());
    //    shipment.setContactPhone(shipmentInfo.getContactPhone());
    //    shipment.setCreateTime(new Date());
    //
    //    // 创建发货明细
    //    List<ShipmentItem> items = new ArrayList<>();
    //
    //    // 使用前端传入的发货明细
    //    if (shipmentInfo.getShipmentItems() != null && !shipmentInfo.getShipmentItems().isEmpty()) {
    //        for (ShipmentItem requestItem : shipmentInfo.getShipmentItems()) {
    //            // 只处理有效的发货项
    //            if (requestItem.getOrderItemId() != null && requestItem.getQuantity() != null
    //                    && requestItem.getQuantity() > 0 && requestItem.getWarehouseId() != null) {
    //                // 找到对应的订单项，获取产品信息
    //                for (OrderItem orderItem : order.getOrderItems()) {
    //                    if (orderItem.getId().equals(requestItem.getOrderItemId())) {
    //                        ShipmentItem shipmentItem = new ShipmentItem();
    //                        shipmentItem.setOrderItemId(orderItem.getId());
    //                        shipmentItem.setProductId(orderItem.getProductId());
    //                        shipmentItem.setProductName(orderItem.getProductName());
    //                        // 使用前端传入的本次发货数量
    //                        shipmentItem.setQuantity(requestItem.getQuantity());
    //                        shipmentItem.setWarehouseId(requestItem.getWarehouseId());
    //                        shipmentItem.setWarehouseName(requestItem.getWarehouseName());
    //                        shipmentItem.setCreateTime(new Date());
    //                        items.add(shipmentItem);
    //                        break;
    //                    }
    //                }
    //            }
    //        }
    //    }
    //
    //    if (items.isEmpty()) {
    //        return Msg.fail("没有有效的发货项");
    //    }
    //
    //    shipment.setShipmentItems(items);
    //
    //    // 保存发货单
    //    int id = shipmentService.createShipment(shipment);
    //
    //    return Msg.success().add("id", id).add("shipmentNo", shipment.getShipmentNo());
    //}
    //
    ///**
    // * 根据订单ID查询发货单列表
    // */
    //@ResponseBody
    //@GetMapping("/getByOrderId/{orderId}")
    //public Msg getByOrderId(@PathVariable Integer orderId) {
    //    List<Shipment> shipments = shipmentService.getShipmentsByOrderId(orderId);
    //    return Msg.success().add("shipments", shipments);
    //}
    //
    ///**
    // * 根据订单ID统计发货单数量
    // */
    //@ResponseBody
    //@GetMapping("/countByOrderId/{orderId}")
    //public Msg countByOrderId(@PathVariable Integer orderId) {
    //    int count = shipmentService.countShipmentsByOrderId(orderId);
    //    return Msg.success().add("count", count);
    //}
    //
    ///**
    // * 根据订单ID获取已发货的商品数量
    // */
    //@ResponseBody
    //@GetMapping("/getShippedItems/{orderId}")
    //public Msg getShippedItems(@PathVariable Integer orderId) {
    //    // 获取订单所有已发货明细项
    //    Map<Integer, Integer> shippedItems = shipmentService.getShippedItemsByOrderId(orderId);
    //    return Msg.success().add("shippedItems", shippedItems);
    //}
    //
    ///**
    // * 检查订单是否有发货单
    // */
    //@ResponseBody
    //@GetMapping("/exists/{orderId}")
    //public Msg exists(@PathVariable Integer orderId) {
    //    boolean exists = shipmentService.existsByOrderId(orderId);
    //    return Msg.success().add("exists", exists);
    //}
}
